//
//  RecommendVC.m
//  MarianasTrench
//
//  Created by 付建国 on 16/3/17.
//  Copyright © 2016年 王爵. All rights reserved.
//

#import "RecommendVC.h"
#import "RecommendModel.h"
#import "RecommendCollectionViewCell.h"
#import "RecommendCollectionReusableView.h"
#import "ListenCollectionViewCell.h"
#import "ScrollViewCollectionViewCell.h"
#import "EditorVC.h"
#import "JGBoutiqueVC.h"

@interface RecommendVC ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, TPDelegat>

@property (nonatomic, strong) UICollectionView *backCollectionV;/**< 底层collectionV */
@property (nonatomic, strong) RecommendModel *recommendModel;/**< 小编推荐 */
@property (nonatomic, strong) RecommendModel *listenModel;/**< 精品单听 */
@property (nonatomic, strong) NSMutableArray *modelArr;/**< 推荐的Model数组 */
@property (nonatomic, strong) NSMutableArray *picArr;/**< 轮播图数组 */
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property (nonatomic, assign) BOOL isUpScroll;/**< 图片数组只取一次 */

@end

@implementation RecommendVC

- (void)dealloc{
    _backCollectionV.delegate = nil;
    _backCollectionV.dataSource = nil;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.recommendModel = [[RecommendModel alloc] init];
    self.listenModel = [[RecommendModel alloc] init];
    self.modelArr = [NSMutableArray array];
    self.picArr = [NSMutableArray array];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.isUpScroll = YES;
    
    [self createView];
    [self addHeader];
    
}
#pragma mark - 创建视图
- (void)createView{
    self.layout = [[UICollectionViewFlowLayout alloc] init];
    _layout.minimumInteritemSpacing = 10;
    _layout.minimumLineSpacing = 10;
    _layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    self.backCollectionV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HETGHT - 153) collectionViewLayout:_layout];
    [self.view addSubview:_backCollectionV];
    self.backCollectionV.delegate = self;
    self.backCollectionV.dataSource = self;
    self.backCollectionV.backgroundColor = [UIColor whiteColor];
    
    // 注册cell和header
    [self.backCollectionV registerClass:[RecommendCollectionViewCell class] forCellWithReuseIdentifier:@"RecommendCollectionViewCell"];
    [self.backCollectionV registerClass:[ListenCollectionViewCell class] forCellWithReuseIdentifier:@"ListenCollectionViewCell"];
    [self.backCollectionV registerClass:[RecommendCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"RecommendCollectionReusableView"];
    // 注册轮播图的cell
    [self.backCollectionV registerClass:[ScrollViewCollectionViewCell class] forCellWithReuseIdentifier:@"ScrollViewCollectionViewCell"];
    
}

#pragma mark - collectionview的协议方法
// 返回分区个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 3 + self.modelArr.count;
}
// 返回cell个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }else if (section == 1) {
        return self.recommendModel.list.count;
    }else if(section == 2){
        return self.listenModel.list.count;
    }else{
        return 3;
    }
}
// 返回不同的cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        ScrollViewCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ScrollViewCollectionViewCell" forIndexPath:indexPath];
            cell.arr = self.picArr;
        return cell;
    }else
        if (indexPath.section == 1) {
        RecommendCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"RecommendCollectionViewCell" forIndexPath:indexPath];
        cell.model = self.recommendModel.list[indexPath.row];
        
        return cell;
    }else if(indexPath.section == 2){
        ListenCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ListenCollectionViewCell" forIndexPath:indexPath];
        cell.model = self.listenModel.list[indexPath.row];
        return cell;
    }else{
        RecommendCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"RecommendCollectionViewCell" forIndexPath:indexPath];
        RecommendModel *recModel = [[RecommendModel alloc] init];
        recModel = self.modelArr[indexPath.section - 3];
        ListModel *model = recModel.list[indexPath.row];
        cell.model = model;
        
        return cell;
    }
}
// 返回头视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if (kind == UICollectionElementKindSectionHeader) {
        RecommendCollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"RecommendCollectionReusableView" forIndexPath:indexPath];
        
        header.Delegat = self;
        
        if (indexPath.section == 0) {
            
        }else if (indexPath.section == 1) {
            
            [header setTitle:self.recommendModel.title];
        }else if(indexPath.section == 2){
            [header setTitle:self.listenModel.title];
        }else{
            RecommendModel *recModel = [[RecommendModel alloc] init];
            recModel = self.modelArr[indexPath.section - 3];
            [header setTitle:recModel.title];
        }
        
        return header;
    }

    return nil;
}
// 返回不同的layout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return CGSizeMake(SCREEN_WIDTH, 200);
    }else if (indexPath.section == 1) {
        return CGSizeMake((SCREEN_WIDTH - 41) / 3, (SCREEN_WIDTH - 40) / 3 + 30);
    }else if(indexPath.section == 2){
        return CGSizeMake(SCREEN_WIDTH - 20, 80);
    }else{
        return CGSizeMake((SCREEN_WIDTH - 41) / 3, (SCREEN_WIDTH - 40) / 3 + 30);
    }
}
// 返回头视图的高度
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return CGSizeMake(SCREEN_WIDTH, 0);
    }else{
        // header的高度
        return CGSizeMake(SCREEN_WIDTH, 30);
    }
}
// cell的点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 2) {
        
        JGBoutiqueVC *boutiqueVC = [[JGBoutiqueVC alloc] init];
        ListModel *model = [[ListModel alloc] init];
        model = self.listenModel.list[indexPath.row];
        boutiqueVC.specialId = model.specialId;
        [self.navigationController pushViewController:boutiqueVC animated:YES];
        
    } else if(indexPath.section == 1){
        
        WJCollectionViewController *WJVC = [[WJCollectionViewController alloc] init];

        ListModel *model = [[ListModel alloc] init];
        
        model = self.recommendModel.list[indexPath.row];
        
        WJVC.str = model.albumId;
        [self.navigationController pushViewController:WJVC animated:YES];
    } else if (indexPath.section >= 3){
        
        WJCollectionViewController *WJVC = [[WJCollectionViewController alloc] init];
        RecommendModel *recModel = [[RecommendModel alloc] init];
        recModel = self.modelArr[indexPath.section - 3];
        ListModel *model = [[ListModel alloc] init];
        model = recModel.list[indexPath.row];
        WJVC.str = model.albumId;
        [self.navigationController pushViewController:WJVC animated:YES];
    }

    
}
#pragma mark - 协议跳转方法
- (void)clicktop:(NSInteger)clickTitle{
    // 根据传递的参数选择跳转的页面
    if (clickTitle == 1) {
        EditorVC *editorVC = [[EditorVC alloc] init];
        editorVC.isOfKind = 1;
        [self.navigationController pushViewController:editorVC animated:YES];
    } else if (clickTitle == 2){
        EditorVC *editorVC = [[EditorVC alloc] init];
        editorVC.isOfKind = 2;
        [self.navigationController pushViewController:editorVC animated:YES];
    }else if(clickTitle <=16 && clickTitle >=3){
        DetailKindController *detailKindVC = [[DetailKindController alloc] init];
        RecommendModel *model = [[RecommendModel alloc] init];
        model = self.modelArr[clickTitle - 3];
        detailKindVC.httpId = model.categoryId;
        [self.navigationController pushViewController:detailKindVC animated:YES];
    }
}


#pragma mark - 数据解析
- (void)dataHander{
    NSString *path = @"http://mobile.ximalaya.com/mobile/discovery/v1/recommends?channel=and-d10&device=android&includeActivity=true&includeSpecial=true&scale=2&version=4.3.26.2";
    [APPTools GETWithURL:path par:nil success:^(id responseObject) {
        // 取出小编推荐
        [self.recommendModel setValuesForKeysWithDictionary:[responseObject objectForKey:@"editorRecommendAlbums"]];
        // 取出精品单听
        [self.listenModel setValuesForKeysWithDictionary:[responseObject objectForKey:@"specialColumn"]];
        // 取出推荐数组
        NSArray *arr = [responseObject objectForKey:@"hotRecommends"][@"list"];
        self.modelArr = [RecommendModel modelHandleWithArray:arr];
        if (_isUpScroll == YES) {
            
            // 取出轮播图数组
            NSDictionary *dic = [responseObject objectForKey:@"focusImages"];
            NSArray *arr2 = [dic objectForKey:@"list"];
            for (NSDictionary *tempDic in arr2) {
                [self.picArr addObject:[tempDic objectForKey:@"pic"]];
            }
        }
        
        
        [_backCollectionV reloadData];
        _isUpScroll = NO;
        [_backCollectionV.mj_header endRefreshing];
        
    } filed:^(NSError *error) {
        NSLog(@"解析失败");
    }];
    
    
}

/**< 下拉刷新 */
- (void)addHeader{
    self.backCollectionV.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
       
        [self dataHander];
        
    }];
    [self.backCollectionV.mj_header beginRefreshing];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
